package GUI;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import javax.swing.JOptionPane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import Excecoes.ExececaoCategoriaVendedorIncorreta;
import Excecoes.TratamentoExcecoes;
import Modulos.ModuloAtualizaItens;
import Modulos.ModuloCalculaComissoes;
import Modulos.ModuloRecuperaItens;
import Utils.UtilMensagens;
import Utils.UtilTransformaStringEmInt;

public class InterfaceComUsuario {

	protected Shell shell;
	private Text nomeArquivo;
	private String fn = new String();
	private Text text;
	private Text text_1;
	private Text text_2;
	private Text text_3;
	private Text text_4;
	private Table table_1;
	private Text text_5;
	private Text text_6;
	private Text text_7;
	private Text text_8;
	private Table table;

	/**
	 * Launch the application.
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		try {
			ModuloRecuperaItens.recuperaItens();
			
			InterfaceComUsuario window = new InterfaceComUsuario();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(479, 381);
		shell.setText("SWT Application");
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBounds(10, 10, 443, 310);
		
		TabFolder tabFolder = new TabFolder(composite, SWT.NONE);
		tabFolder.setBounds(0, 0, 450, 325);
		
		TabItem tbtmProdutos = new TabItem(tabFolder, SWT.NONE);
		tbtmProdutos.setText("Produtos");
		
		Composite composite_1 = new Composite(tabFolder, SWT.NONE);
		tbtmProdutos.setControl(composite_1);
		
		nomeArquivo = new Text(composite_1, SWT.BORDER);
		nomeArquivo.setBounds(10, 22, 168, 25);
		
		Button btnUploadArquivo = new Button(composite_1, SWT.NONE);
		btnUploadArquivo.setBounds(10, 76, 167, 25);
		btnUploadArquivo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dlg = new FileDialog(shell, SWT.OPEN);
				fn = dlg.open();
				nomeArquivo.setText(fn);
			}
		});
		btnUploadArquivo.setText("Selecionar Arquivo");
		
		Button btnOk = new Button(composite_1, SWT.NONE);
		btnOk.setBounds(171, 150, 75, 25);
		btnOk.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					ModuloAtualizaItens.atualizaItens(fn);
				} catch (IOException e1) {
					TratamentoExcecoes.trataExcecaoLeituraEscritaDeArquivo();
					return;
				} catch (ParseException e1) {
					TratamentoExcecoes.trataExcecaoParseData();
					return;
				} catch (ExececaoCategoriaVendedorIncorreta e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnOk.setText("Ok");
		
		TabItem tbtmVendedores = new TabItem(tabFolder, SWT.NONE);
		tbtmVendedores.setText("Vendedores");
		
		Composite composite_2 = new Composite(tabFolder, SWT.NONE);
		tbtmVendedores.setControl(composite_2);
		
		text = new Text(composite_2, SWT.BORDER);
		text.setBounds(20, 10, 150, 21);
		
		Button btnNewButton = new Button(composite_2, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dlg = new FileDialog(shell, SWT.OPEN);
				fn = dlg.open();
				text.setText(fn);
			}
		});
		btnNewButton.setBounds(27, 59, 143, 25);
		btnNewButton.setText("Selecionar Arquivo");
		
		Button btnNewButton_1 = new Button(composite_2, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					ModuloAtualizaItens.atualizaItens(fn);
				} catch (IOException e1) {
					TratamentoExcecoes.trataExcecaoLeituraEscritaDeArquivo();
					return;
				} catch (ParseException e1) {
					TratamentoExcecoes.trataExcecaoParseData();
					return;
				} catch (ExececaoCategoriaVendedorIncorreta e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnNewButton_1.setBounds(156, 134, 75, 25);
		btnNewButton_1.setText("Ok");
		
		TabItem tbtmPreo = new TabItem(tabFolder, SWT.NONE);
		tbtmPreo.setText("Pre\u00E7o");
		
		Composite composite_3 = new Composite(tabFolder, SWT.NONE);
		tbtmPreo.setControl(composite_3);
		
		text_1 = new Text(composite_3, SWT.BORDER);
		text_1.setBounds(24, 36, 105, 21);
		
		Button btnNewButton_2 = new Button(composite_3, SWT.NONE);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dlg = new FileDialog(shell, SWT.OPEN);
				fn = dlg.open();
				text.setText(fn);
			}
		});
		btnNewButton_2.setBounds(24, 87, 105, 21);
		btnNewButton_2.setText("Selecionar Pre\u00E7o");
		
		Button btnOk_1 = new Button(composite_3, SWT.NONE);
		btnOk_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					ModuloAtualizaItens.atualizaItens(fn);
				} catch (IOException e1) {
					TratamentoExcecoes.trataExcecaoLeituraEscritaDeArquivo();
					return;
				} catch (ParseException e1) {
					TratamentoExcecoes.trataExcecaoParseData();
					return;
				} catch (ExececaoCategoriaVendedorIncorreta e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnOk_1.setBounds(132, 156, 75, 25);
		btnOk_1.setText("OK");
		
		TabItem tbtmVendas = new TabItem(tabFolder, SWT.NONE);
		tbtmVendas.setText("Vendas");
		
		Composite composite_4 = new Composite(tabFolder, SWT.NONE);
		tbtmVendas.setControl(composite_4);
		
		text_2 = new Text(composite_4, SWT.BORDER);
		text_2.setBounds(10, 10, 121, 21);
		
		Button btnNewButton_3 = new Button(composite_4, SWT.NONE);
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dlg = new FileDialog(shell, SWT.OPEN);
				fn = dlg.open();
				text.setText(fn);
			}
		});
		btnNewButton_3.setBounds(11, 51, 120, 25);
		btnNewButton_3.setText("Selecionar Arquivo");
		
		Button btnNewButton_4 = new Button(composite_4, SWT.NONE);
		btnNewButton_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					ModuloAtualizaItens.atualizaItens(fn);
				} catch (IOException e1) {
					TratamentoExcecoes.trataExcecaoLeituraEscritaDeArquivo();
					return;
				} catch (ParseException e1) {
					TratamentoExcecoes.trataExcecaoParseData();
					return;
				} catch (ExececaoCategoriaVendedorIncorreta e1) {
					TratamentoExcecoes.trataExececaoCategoriaVendedorIncorreta();
				}
			}
		});
		btnNewButton_4.setBounds(158, 123, 75, 25);
		btnNewButton_4.setText("OK");
		
		TabItem tbtmComissesms = new TabItem(tabFolder, SWT.NONE);
		tbtmComissesms.setText("Vendedor/M\u00EAs");
		
		Composite composite_5 = new Composite(tabFolder, SWT.NONE);
		tbtmComissesms.setControl(composite_5);
		composite_5.setLayout(new FormLayout());
		
		text_3 = new Text(composite_5, SWT.BORDER);
		FormData fd_text_3 = new FormData();
		fd_text_3.top = new FormAttachment(0, 38);
		fd_text_3.left = new FormAttachment(0, 97);
		text_3.setLayoutData(fd_text_3);
		
		text_4 = new Text(composite_5, SWT.BORDER);
		FormData fd_text_4 = new FormData();
		fd_text_4.top = new FormAttachment(text_3, 0, SWT.TOP);
		fd_text_4.left = new FormAttachment(text_3, 51);
		text_4.setLayoutData(fd_text_4);
		
		Button btnNewButton_5 = new Button(composite_5, SWT.NONE);
		btnNewButton_5.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int mes;
				int ano;
				try{
					mes = UtilTransformaStringEmInt.transformaTextoEmInt(text_3.getText());
					ano = UtilTransformaStringEmInt.transformaTextoEmInt(text_4.getText());
				}catch(NumberFormatException e1){
					TratamentoExcecoes.trataExcecaoParseInt();
					return;
				}
				ModuloCalculaComissoes.calculaComissao(ano, mes);
				configuraTabela(ModuloCalculaComissoes.totaisComissaoVenda, table_1);
			}
		});
		FormData fd_btnNewButton_5 = new FormData();
		fd_btnNewButton_5.left = new FormAttachment(0, 186);
		fd_btnNewButton_5.right = new FormAttachment(100, -192);
		btnNewButton_5.setLayoutData(fd_btnNewButton_5);
		btnNewButton_5.setText("OK");
		
		table_1 = new Table(composite_5, SWT.BORDER | SWT.FULL_SELECTION);
		fd_btnNewButton_5.bottom = new FormAttachment(table_1, -5);
		FormData fd_table_1 = new FormData();
		fd_table_1.bottom = new FormAttachment(100, -31);
		fd_table_1.top = new FormAttachment(0, 101);
		fd_table_1.left = new FormAttachment(100, -356);
		fd_table_1.right = new FormAttachment(100, -64);
		table_1.setLayoutData(fd_table_1);
		table_1.setHeaderVisible(true);
		table_1.setLinesVisible(true);

		TableColumn tblclmnVendedor = new TableColumn(table_1, SWT.NONE);
		tblclmnVendedor.setWidth(151);
		tblclmnVendedor.setText("Vendedor");
		
		TableColumn tblclmnVendas = new TableColumn(table_1, SWT.NONE);
		tblclmnVendas.setWidth(60);
		tblclmnVendas.setText("Vendas");
		
		TableColumn tblclmnComisses = new TableColumn(table_1, SWT.NONE);
		tblclmnComisses.setWidth(72);
		tblclmnComisses.setText("Comiss\u00E3o");
		
		Label lblMs = new Label(composite_5, SWT.NONE);
		FormData fd_lblMs = new FormData();
		fd_lblMs.bottom = new FormAttachment(text_3, -6);
		fd_lblMs.left = new FormAttachment(text_3, 0, SWT.LEFT);
		lblMs.setLayoutData(fd_lblMs);
		lblMs.setText("M\u00EAs");
		
		Label lblAno = new Label(composite_5, SWT.NONE);
		FormData fd_lblAno = new FormData();
		fd_lblAno.top = new FormAttachment(lblMs, 0, SWT.TOP);
		fd_lblAno.left = new FormAttachment(lblMs, 104);
		lblAno.setLayoutData(fd_lblAno);
		lblAno.setText("Ano");
		
		TabItem tbtmVendedoracumulado = new TabItem(tabFolder, SWT.NONE);
		tbtmVendedoracumulado.setText("Vendedor/Acumulado");
		
		Composite composite_6 = new Composite(tabFolder, SWT.NONE);
		tbtmVendedoracumulado.setControl(composite_6);
		
		text_5 = new Text(composite_6, SWT.BORDER);
		text_5.setBounds(36, 36, 76, 21);
		
		text_6 = new Text(composite_6, SWT.BORDER);
		text_6.setBounds(185, 36, 76, 21);
		
		Button btnNewButton_6 = new Button(composite_6, SWT.NONE);
		btnNewButton_6.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int mesInicial;
				int mesFinal;
				int anoInicial;
				int anoFinal;
				try{
					mesInicial = UtilTransformaStringEmInt.transformaTextoEmInt(text_5.getText());
					mesFinal = UtilTransformaStringEmInt.transformaTextoEmInt(text_7.getText());
					anoInicial = UtilTransformaStringEmInt.transformaTextoEmInt(text_6.getText());
					anoFinal = UtilTransformaStringEmInt.transformaTextoEmInt(text_8.getText());
				}catch(NumberFormatException e1){
					JOptionPane.showMessageDialog(null, UtilMensagens.mensagemErroParseInt);
					return;
				}
				ModuloCalculaComissoes.calculaComissao(anoInicial, mesInicial, anoFinal, mesFinal);
				configuraTabela(ModuloCalculaComissoes.totaisComissaoVenda, table);
			}
		});
		btnNewButton_6.setBounds(301, 59, 75, 25);
		btnNewButton_6.setText("New Button");
		
		text_7 = new Text(composite_6, SWT.BORDER);
		text_7.setBounds(36, 85, 76, 21);
		
		text_8 = new Text(composite_6, SWT.BORDER);
		text_8.setBounds(185, 85, 76, 21);
		
		Label lblMesInicial = new Label(composite_6, SWT.NONE);
		lblMesInicial.setBounds(36, 15, 55, 15);
		lblMesInicial.setText("Mes Inicial");
		
		Label lblMesFinal = new Label(composite_6, SWT.NONE);
		lblMesFinal.setBounds(36, 64, 55, 15);
		lblMesFinal.setText("Mes Final");
		
		Label lblAnoInicial = new Label(composite_6, SWT.NONE);
		lblAnoInicial.setBounds(187, 15, 55, 15);
		lblAnoInicial.setText("Ano Inicial");
		
		Label lblAnoFinal = new Label(composite_6, SWT.NONE);
		lblAnoFinal.setBounds(185, 64, 55, 15);
		lblAnoFinal.setText("Ano Final");
		
		table = new Table(composite_6, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setBounds(36, 122, 292, 165);
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(151);
		tableColumn.setText("Vendedor");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(60);
		tableColumn_1.setText("Vendas");
		
		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(72);
		tableColumn_2.setText("Comiss\u00E3o");
	}

	public void configuraTabela(List<String[]> dados, Table table){
		table.removeAll();
		for(String[] dado : dados){
			TableItem item = new TableItem(table, SWT.NULL);
			item.setText(0, dado[0]);
			item.setText(1, dado[1]);
			item.setText(2, dado[2]);
		}
	}
}
